package pATT.profilingAlgorithm.PersonalSearcher;


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;

public class FileHashTable extends Hashtable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public FileHashTable(){
    	super();
	}

    public FileHashTable(int initialCapacity){
    	super(initialCapacity);
	}

    @SuppressWarnings("unchecked")
	public void load(String file) throws IOException {
    	BufferedReader in=new BufferedReader(new FileReader(file));
        String line=in.readLine();
        while (line!=null){
        	StringTokenizer token=new StringTokenizer(line,"#");
            String key=token.nextToken();
            String value=token.nextToken();
            this.put(key,value);
            line=in.readLine();
		}
        in.close();
	}

    @SuppressWarnings("unchecked")
	public void load(String file, String sep) throws IOException{
    	BufferedReader in=new BufferedReader(new FileReader(file));
        String line=in.readLine();
        while (line!=null){
        	StringTokenizer token=new StringTokenizer(line,sep);
            String key=token.nextToken();
            String value=token.nextToken();
            this.put(key,value);
            line=in.readLine();
		}
        in.close();
	}

    public void download(String file) throws IOException{
    	DataOutputStream out=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
        Enumeration allKeys=this.keys();
        while (allKeys.hasMoreElements()){
        	String key=(String)allKeys.nextElement();
            out.writeBytes(key+"#"+(String)this.get(key));
            out.writeBytes("\n");
		}
        out.close();
	}
}
